/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Dictator.java
 *
 * Created on Feb 10, 2009, 11:03:06 AM
 */

/**
 *
 * @author Himanshu  Sharma
 */
import java.awt.event.MouseListener ;
import java.awt.event.MouseEvent ;
import java.awt.event.InputEvent ;
public class Dictator extends javax.swing.JFrame {

    int disable = 0;
    Double money_Won = 0.0, init_money = 0.0, money_given = 0.0 ;
    Game G;
    /** Creates new form Dictator */

    Dictator( double init_money ){
        this.init_money = init_money;
        initComponents();

        G = new Game();
        
        l3.setText(Double.toString(init_money));
        this.setVisible(true);
        t1.setText(Double.toString(money_given));
        addMouseListener( new Dictator.CheckMouse() ) ;
    }
    public Dictator() {
        init_money = 10.0;
        initComponents();
        G = new Game();
        
        l3.setText(Double.toString(init_money));
        this.setVisible(true);
        t1.setText(Double.toString(money_given));
        addMouseListener( new Dictator.CheckMouse() ) ;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        l1 = new javax.swing.JLabel();
        l2 = new javax.swing.JLabel();
        l4 = new javax.swing.JLabel();
        t1 = new javax.swing.JTextField();
        b1 = new javax.swing.JButton();
        l5 = new javax.swing.JLabel();
        l3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        l1.setFont(new java.awt.Font("Tahoma", 0, 14));
        l1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        l1.setText("Dictator");

        l2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        l2.setText("You have $");

        l4.setText("How much would you like to give to Player 2 ($)");

        t1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                t1ActionPerformed(evt);
            }
        });

        b1.setText("Give");
        b1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b1ActionPerformed(evt);
            }
        });

        l5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(390, Short.MAX_VALUE)
                .addComponent(l3)
                .addGap(444, 444, 444))
            .addGroup(layout.createSequentialGroup()
                .addGap(232, 232, 232)
                .addComponent(l5, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(391, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(313, 313, 313)
                .addComponent(l1)
                .addContainerGap(472, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(107, 107, 107)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(158, 158, 158)
                                    .addComponent(l2, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(l4)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(t1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(b1)
                            .addGap(148, 148, 148)))
                    .addContainerGap(309, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(l1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                .addComponent(l3)
                .addGap(59, 59, 59)
                .addComponent(l5, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(81, 81, 81))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(76, 76, 76)
                    .addComponent(l2)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(t1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(l4))
                    .addGap(115, 115, 115)
                    .addComponent(b1)
                    .addContainerGap(34, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void b1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b1ActionPerformed
            if( b1.getText().equals("Next") ){
                try{
                    G.writeLog("Dictator: $" + money_Won);
                }catch(Exception e){
                    e.printStackTrace();
                }
                setVisible(false);
                G.free = true; 
            }

            if(b1.getText().equals("Give")){
                money_Won = init_money - Double.parseDouble(t1.getText());
                l5.setText(String.format("You get %.2f", money_Won));
                b1.setText("Next");
            }
        // TODO add your handling code here:
}//GEN-LAST:event_b1ActionPerformed

    private void t1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_t1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_t1ActionPerformed

    @Override
    public synchronized void addMouseListener(MouseListener l) {
        super.addMouseListener(l);
    }



    class CheckMouse implements MouseListener{
        public void mousePressed(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
        public void mouseClicked(MouseEvent e) {
            int modifiers = e.getModifiers();
            if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
                money_given += 1.0 ;
                t1.setText(Double.toString(money_given));
            }
            if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {}
            if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
                if( b1.getText().equals("Next") ){
                try{
                    G.writeLog("Dictator: $" + money_Won);
                }catch(Exception e1){
                    e1.printStackTrace();
                }
                setVisible(false);
                G.free = true;
            }

            if(b1.getText().equals("Give")){
                money_Won = init_money - Double.parseDouble(t1.getText());
                l5.setText(String.format("You get %.2f", money_Won));
                b1.setText("Next");
            }
        }
        }
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Dictator().setVisible(true);

            }
        });
    }



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton b1;
    private javax.swing.JLabel l1;
    private javax.swing.JLabel l2;
    private javax.swing.JLabel l3;
    private javax.swing.JLabel l4;
    private javax.swing.JLabel l5;
    private javax.swing.JTextField t1;
    // End of variables declaration//GEN-END:variables

}
